load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:defs.bzl", "rust_ic_test_suite")
load("//bazel:prost.bzl", "generated_files_check")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "protos",
    srcs = glob(["proto/**/*.proto"]),
)

# See rs/nervous_system/feature_test.md
DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-dummy-getrandom-for-wasm",
    "//packages/ic-secp256k1",
    "//rs/crypto/sha2",
    "//rs/ledger_suite/icp:icp_ledger",
    "//rs/nervous_system/common",
    "//rs/nns/common",
    "//rs/nns/constants",
    "//rs/nns/governance/api",
    "//rs/nns/gtc_accounts",
    "//rs/rust_canisters/dfn_candid",
    "//rs/rust_canisters/dfn_core",
    "//rs/types/base_types",
    "@crate_index//:build-info",
    "@crate_index//:candid",
    "@crate_index//:hex",
    "@crate_index//:hex-literal",
    "@crate_index//:lazy_static",
    "@crate_index//:prost",
    "@crate_index//:rand",
    "@crate_index//:serde",
    "@crate_index//:sha3",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "//rs/nervous_system/common/build_metadata",
]

BUILD_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:build-info-build",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/nns/gtc/protobuf_generator:lib",
    "//rs/test_utilities/compare_dirs",
    "@crate_index//:candid_parser",
    "@crate_index//:tempfile",
] + select({
    "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
    "//conditions:default": [
        "//rs/rust_canisters/canister_test",
        "//rs/nns/test_utils",
    ],
})

MACRO_DEV_DEPENDENCIES = select({
    "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
    "//conditions:default": [
        "//rs/nns/test_utils_macros",
    ],
})

ALIASES = {}

LIB_SRCS = glob(
    ["src/**"],
    # Ensures that we do not need to rebuild just because a _test.rs file
    # changed.
    exclude = ["**/*tests.rs"],
)

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    aliases = ALIASES,
    data = [],  # build script data (e.g. template files) goes here
    version = "0.9.0",
    deps = BUILD_DEPENDENCIES,
)

rust_library(
    name = "gtc",
    srcs = LIB_SRCS,
    aliases = ALIASES,
    crate_name = "ic_nns_gtc",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES + [":build_script"],
)

rust_library(
    name = "gtc--test_feature",
    srcs = LIB_SRCS,
    aliases = ALIASES,
    crate_features = ["test"],
    crate_name = "ic_nns_gtc",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES + [":build_script"],
)

rust_canister(
    name = "genesis-token-canister",
    srcs = ["canister/canister.rs"],
    aliases = ALIASES,
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":canister/gtc.did",
    deps = DEPENDENCIES + [
        ":build_script",
        ":gtc",
    ],
)

rust_test(
    name = "gtc_test",
    aliases = ALIASES,
    crate = ":gtc",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":build_script"],
)

rust_test(
    name = "canister_unit_test",
    srcs = glob(["canister/**/*.rs"]),
    compile_data = ["canister/gtc.did"],
    crate_root = "canister/canister.rs",
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [
        ":build_script",
        ":gtc",
    ],
)

rust_ic_test_suite(
    name = "gtc_integration_test",
    srcs = glob(
        ["tests/**/*.rs"],
        exclude = ["tests/check_generated_files.rs"],
    ),
    aliases = ALIASES,
    data = [
        "//rs/nns/gtc:genesis-token-canister",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/nns/gtc",
        "GENESIS_TOKEN_CANISTER_WASM_PATH": "$(rootpath //rs/nns/gtc:genesis-token-canister)",
    },
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = [":gtc"] + DEPENDENCIES + DEV_DEPENDENCIES + [":build_script"],
)

generated_files_check(
    name = "check_generated_files",
    srcs = ["tests/check_generated_files.rs"],
    data = glob(["src/gen/**/*.rs"]) + [
        ":protos",
        "//rs/nns/common:protos",
        "//rs/types/base_types:protos",
    ],
    manifest_dir = "rs/nns/gtc",
    deps = [
        # Keep sorted.
        "//rs/nns/gtc/protobuf_generator:lib",
        "//rs/test_utilities/compare_dirs",
        "@crate_index//:tempfile",
    ],
)
